package advanced.datastructure;

import structure5.Association;

public class AddressStudents {
    public MyVector<Association> addressStudentAssoc = new MyVector<Association>();
    
	public void updateCount(String inputAddress)
	{
		int i;
		for (i = 0; i < addressStudentAssoc.size(); i++)
		{
			Association addressStudents;
			String address;
			
			// get the association
			addressStudents = (Association)addressStudentAssoc.get(i);
			// get the word from the association
			address = (String)addressStudents.getKey();
			
			if (address.equals(inputAddress))
			{ 
				// match: increment integer in association
				Integer studentCount = (Integer)addressStudents.getValue();
				addressStudents.setValue(new Integer(studentCount.intValue() + 1));
				break;
			}
		}
		// mismatch: add new word, frequency 1.
		if (i == addressStudentAssoc.size())
		{
			addressStudentAssoc.add(new Association(inputAddress,new Integer(1)));
		}
	}
	
	public Association getAddressSharedByMost()
	{
		addressStudentAssoc.sort(new AddressStudentsAssocComparator());
		for(int i=addressStudentAssoc.size()-1; i>=0; i--)
		{
			if(!((String)addressStudentAssoc.get(i).getKey()).equals("UNKNOWN"))
			{
				return addressStudentAssoc.get(i);
			}
		}
		return addressStudentAssoc.getLast();
	}
    
}
